import request from "@/utils/http_request";
import { ElMessage } from "element-plus";
import { LoginDto } from "./types/login.dto";
export const login = (data: LoginDto) => {
  return request({
    url: "/user/login",
    data,
    isToken: false,
    method: "post",
  });
};

//获取验证码
var pre_captcha_time = 0;// 上次验证码请求时间-unix时间戳
export const getCaptcha = () => {
  // 请求时间在2s内，则不再重复请求
  if (Date.now() - pre_captcha_time < 2000) {
    // 弹出请求过于频繁的窗口提示
    ElMessage({
      message: "请求过于频繁，请稍后再试",
      type: "error",
      duration: 2 * 1000,
    });
    return Promise.reject("请求过于频繁，请稍后再试");
  }
  pre_captcha_time = Date.now();
  return request({
    url: "/code/captcha",
    isToken: false,
    loading: false,
    method: "get",
  });
};

//退出登录
export const logout = () => {
  return request({
    url: "/user/logout",
    method: "post",
  });
};
